package com.zrrd.tree;


import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    public static class Node {
        Object data;
        Node lChild;
        Node rChild;

        public Node(Object data) {
            this.data = data;
        }
    }

    /**
     * 层序遍历
     */
    public void levelOrderTraversal(Node node) {
        Queue<Node> nodeQueue = new LinkedList<>();
        nodeQueue.offer(node);
        while (!nodeQueue.isEmpty()) {
            Node node1 = nodeQueue.poll();
            System.out.println(node1.data);
            if (node1.lChild != null) {
                nodeQueue.offer(node1.lChild);
            }
            if (node1.rChild != null) {
                nodeQueue.offer(node1.rChild);
            }
        }
    }

    /**
     * 二叉树先序遍历
     */
    public void firstOrderTraversal(Node root) {
        System.out.println(root.data);
        if (root.lChild != null) {
            firstOrderTraversal(root.lChild);
        }
        if (root.rChild != null) {
            firstOrderTraversal(root.rChild);
        }
    }

    /**
     * 二叉树中序遍历
     */
    public void middleOrderTraversal(Node root) {

        if (root.lChild != null) {
            middleOrderTraversal(root.lChild);
        }
        System.out.println(root.data);
        if (root.rChild != null) {
            middleOrderTraversal(root.rChild);
        }
    }

    /**
     * 二叉树后序遍历
     */
    public void lastOrderTraversal(Node root) {

        if (root.lChild != null) {
            lastOrderTraversal(root.lChild);
        }

        if (root.rChild != null) {
            lastOrderTraversal(root.rChild);
        }
        System.out.println(root.data);
    }

}
